package com.huihui.activity.mine;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;

import com.google.gson.reflect.TypeToken;
import com.huihui.R;
import com.huihui.adapter.MineZanAdapter;
import com.huihui.adapter.ZanAdapter;
import com.huihui.base.BaseActivity;
import com.huihui.bean.Goods;
import com.huihui.bean.MineZan;
import com.huihui.datamanager.AppManager;
import com.huihui.eventbus.PublicRefrash;
import com.huihui.network.HttpContants;
import com.huihui.network.OkHttpRequest;
import com.huihui.network.base.CallBackUtil;
import com.huihui.widget.SwipeRecyclerView;
import com.huihui.widget.footerView.SimpleFooterView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;

/**
 * 我的点赞
 */
public class MineZanActivity extends BaseActivity {

    private SwipeRecyclerView recyclerView;
    private int pageIndex = 1;
    private int pageNum = 10;
    private ZanAdapter adapter;
    private List<MineZan> mList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mine_zan);
        EventBus.getDefault().register(this);
//        getSupportActionBar().setTitle(R.string.my_zan);
        titleView.setText(R.string.my_zan);
        initView();
        initData();
        initListener();
    }


    private void initListener() {
        recyclerView.setOnLoadListener(new SwipeRecyclerView.OnLoadListener() {
            @Override
            public void onRefresh() {
                pageIndex = 1;
                initData();
            }

            @Override
            public void onLoadMore() {
                pageIndex ++;
                initData();
            }
        });
    }

    private void initData() {
        OkHttpRequest.getInstance().mineList(HttpContants.collection_list, AppManager.getUserInfo().getUser_id(), 10, pageIndex, new CallBackUtil.CallBackString() {
            @Override
            public void onFailure(Call call, Exception e) {
                recyclerView.setRefreshing(false);
            }

            @Override
            public void onResponse(String response) {
                recyclerView.setRefreshing(false);
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    if(jsonObject != null && jsonObject.has(HttpContants.DATA)){
                        List<MineZan> list = AppManager.getGson().fromJson(jsonObject.getString(HttpContants.DATA),new TypeToken<List<MineZan>>(){}.getType());
                        if(list != null){
                            if(pageIndex == 1){
                                mList.clear();
                                mList.addAll(list);
                                adapter.notifyDataSetChanged();
                            }else{
                                mList.addAll(list);
                                adapter.notifyDataSetChanged();
                            }
                            if(list.size() < 10){
                                recyclerView.setLoadMoreEnable(false);
                            }else{
                                recyclerView.setLoadMoreEnable(true);
                            }
                        }else{
                            recyclerView.setLoadMoreEnable(false);
                        }
                    }else {
                        recyclerView.setLoadMoreEnable(false);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });

    }

    private void initView() {
        recyclerView = (SwipeRecyclerView) findViewById(R.id.recycler);
        recyclerView.getSwipeRefreshLayout()
                .setColorSchemeColors(getResources().getColor(R.color.colorPrimary));
        setRecycleView(recyclerView);
        recyclerView.setRefreshing(true);

        adapter = new ZanAdapter(this,mList);
        recyclerView.setAdapter(adapter);

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void refrashData(PublicRefrash publicRefrash) {
        if (publicRefrash != null) {
            if (adapter.getItemCount() >= publicRefrash.getPosition()) {
                MineZan mineZan = publicRefrash.getMineZan();
                if(mineZan == null)return;
                int p = publicRefrash.getPosition();
                if (mList.get(p).getRelation_id() == mineZan.getRelation_id()) {
                    mList.get(p).setComment_num(mineZan.getComment_num());
                    mList.get(p).setCollection_num(mineZan.getCollection_num());
                    mList.get(p).setIs_like(mineZan.getIs_like());
                    mList.get(p).setShare_num(mineZan.getShare_num());
                    adapter.notifyItemChanged(p);
//                    pageIndex = 1;
//                    initData();
                }

            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

//    private void removeZan(int p) {
//       final  MineZan mineZan = mList.get(p);
//        int class_id = mineZan.getClass_id();
////        int comment_id = mineZan.getComment_id();
//        int comment_id = 0;
//        long relation_id = mineZan.getRelation_id();
//
//        OkHttpRequest.getInstance().removeCollection(HttpContants.removeCollection, AppManager.getUserInfo().getUser_id(),
//                relation_id, class_id, comment_id, new CallBackUtil.CallBackString() {
//                    @Override
//                    public void onFailure(Call call, Exception e) {
//
//                    }
//
//                    @Override
//                    public void onResponse(String response) {
//                        try {
//                            JSONObject productObject = new JSONObject(response);
//                            if (productObject != null && productObject.has(HttpContants.STATE) && productObject.getInt(HttpContants.STATE) == 10000) {
//
//                                mineZan.setCollection_num(mineZan.getCollection_num() - 1);
//                                mineZan.setIs_like(0);
////                                pageIndex = 1;
////                                initData();
//                                adapter.notifyDataSetChanged();
//                            }
//                        } catch (JSONException e) {
//                            e.printStackTrace();
//                        }
//                    }
//                });
//    }
}
